#include "includes.h"

struct host_entry _hosts;
struct host_entry _clients;

int host_count = 0;
int curr_host_count = 0;
int my_id;
//int _view = 0;

void init_host_list() {
	INIT_LIST_HEAD(&_hosts.list);
	INIT_LIST_HEAD(&_clients.list);
}


struct host_entry *add_host(char *host_name, int sock) {
	struct host_entry *entry;
	entry = (struct host_entry *) malloc(sizeof(struct host_entry));
	entry->host_name = (char *) malloc(strlen(host_name) + 1);
	strncpy(entry->host_name, host_name, strlen(host_name));
	entry->id = host_count;
	entry->socket = sock;
	
	//printf("Server %s ID %d\n",entry->host_name,entry->id);
	
	list_add_tail(&(entry->list), &(_hosts.list));
	
	host_count ++; //Increment host count
}

struct host_entry *add_client(int id, int sock) {
	struct host_entry *entry;
	
	if(get_client_socket(id) == -1) {
		entry = (struct host_entry *) malloc(sizeof(struct host_entry));
		entry->id = id;
		entry->socket = sock;
	
		list_add_tail(&(entry->list), &(_clients.list));
	}else{
		struct host_entry *tmp;
		struct list_head *pos;
	
		list_for_each(pos, &_clients.list) {
			tmp = list_entry(pos, struct host_entry, list);
			if(tmp->id == id) {
				tmp->socket = sock;
			}
	}
		
	}
}

int get_client_socket(int id) {
	struct host_entry *tmp;
	struct list_head *pos;
	
	list_for_each(pos, &_clients.list) {
		tmp = list_entry(pos, struct host_entry, list);
		if(tmp->id == id) {
			return tmp->socket;
		}
	}
	
	return -1;
}

void remove_host(char *host_name){

	struct host_entry *tmp;
	struct list_head *pos, *q;
	
	list_for_each_safe(pos, q, &_hosts.list) {
		tmp = list_entry(pos, struct host_entry, list);
		
		if(strcmp(tmp->host_name, host_name) == 0) {
			list_del(pos);
			curr_host_count--;
		}
	}
}


int get_host_id(char *host_name) {
	struct host_entry *tmp;
	struct list_head *pos;
	
	list_for_each(pos, &_hosts.list) {
		tmp = list_entry(pos, struct host_entry, list);
		if(strcmp(tmp->host_name, host_name) == 0) {
			return tmp->id;
		}
	}
	
	return -1;
}

char *get_host_name(int id) {
	struct host_entry *tmp;
	struct list_head *pos;
	
	list_for_each(pos, &_hosts.list) {
		tmp = list_entry(pos, struct host_entry, list);
		if(id == tmp->id) {
			return tmp->host_name;
		}
	}
	
	return NULL;
}

int get_host_socket(char *host_name){
	struct host_entry *tmp;
	struct list_head *pos;
	
	list_for_each(pos, &_hosts.list) {
		tmp = list_entry(pos, struct host_entry, list);
		if(strcmp(tmp->host_name, host_name) == 0) {
			return tmp->socket;
		}
	}
	
	return -1;
}

void set_host_socket(char* host_name, int sock){
	struct host_entry *tmp;
	struct list_head *pos;
	
	list_for_each(pos, &_hosts.list) {
		tmp = list_entry(pos, struct host_entry, list);
		if(strcmp(tmp->host_name, host_name) == 0) {
			tmp->socket = sock;
		}
	}
}

char* get_host_by_socket(int sock){
	struct host_entry *tmp;
	struct list_head *pos;
	
	list_for_each(pos, &_hosts.list) {
		tmp = list_entry(pos, struct host_entry, list);
		if(tmp->socket == sock) {
			return tmp->host_name;
		}
	}
	
	return NULL;
}
